package com.hnkywl.sts.service.cwgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cwgl.SkdrDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.MoneyUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by HL on 2015/9/23.
 */
@Service
public class SkdrService {
    private GsjgDao gsjgDao;
    private SkdrDao skdrDao;

    @Transactional
    public String findAllSkdr(Date skshStart, Date skshEnd, String bm, String bzjdrpjlxMc, String sbgsMc, Long sbgsId, String gldwMc, String zt, Integer cwsfZt, String skr, int pageNo, int pageSize) {
        Long nbgsIds = null;
        if (sbgsId != null && sbgsMc.equals(gsjgDao.get(sbgsId).getJc())) {
            nbgsIds = nbgsIds;
        }
        Page<Sfk> skdrPage = this.skdrDao.findSkdrList(skshStart, skshEnd, bm, bzjdrpjlxMc, sbgsMc, nbgsIds, gldwMc, zt, cwsfZt, skr, pageNo, pageSize);
        List<Sfk> skdrList = skdrPage.getResult() == null ? new ArrayList<Sfk>() : skdrPage.getResult();
        if (skdrList.size() > 0) {
            skdrList = getCzztBySkdr(skdrList);
        }
        String skdrJson = BaseUtils.fetchCollectionToJson(skdrList, "id", "bm", "sfkrq", "sfklx", "sfkrq", "czzt", "gldw.qc", "kxyt.z", "kxsy.z", "fkfs.z", "sfje", "pjlx.z", "kxlx", "sbgs.jc", "sbr.xm", "bz", "zt", "shrq", "khfksj", "pjhm",
                "acctyear", "uuid", "voucherId", "voucherNum", "insertFlag", "deleteFlag");
        return "{\"total\":" + skdrPage.getTotalCount() + ",\"rows\":[" + skdrJson + "]}";
    }

    public List<Sfk> getCzztBySkdr(List<Sfk> sfkList) {
        for (Sfk p : sfkList) {
            String czzt = "";
            if (p.getInsertFlag() == 1 && p.getDeleteFlag() == 1) {
                czzt = "wdr";
            } else if (p.getInsertFlag() == 1 && p.getDeleteFlag() == 3) {
                czzt = "sccg";
            } else if (p.getInsertFlag() == 3 && p.getDeleteFlag() == 1) {
                czzt = "drcg";
            } else if (p.getInsertFlag() == 2 && p.getDeleteFlag() == 1 || p.getInsertFlag() == 2 && p.getDeleteFlag() == 3) {
                czzt = "drsb";
            } else if (p.getInsertFlag() == 3 && p.getDeleteFlag() == 2) {
                czzt = "scsb";
            } else {
                czzt = "null";
            }
            p.setCzzt(czzt);
        }
        return sfkList;
    }

    @Transactional
    public Sfk findSkdrDetail(Long skdrId) {
        return this.skdrDao.get(skdrId);
    }


    @Transactional
    public Sfk findskdrDetail(Long id) {
        return this.skdrDao.getSkdr(id);
    }

    public Map<String, Object> getVoucherMaster(Long skdrId, Integer attachment, String uuid, String year, String mouth, Yg yg) {
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        String systemDate = simple.format(new Date());
        Map<String, Object> voucherMaster = new HashMap<String, Object>();
        Sfk cwsf = this.skdrDao.get(skdrId);
        voucherMaster.put("id", uuid);
        voucherMaster.put("vchrtypecode", "记");//凭证类别
        voucherMaster.put("acctyear", year != null ? year : "");//会计年度
        voucherMaster.put("acctperiod", mouth != null ? mouth : "");//会计期间
        voucherMaster.put("createdate", systemDate);//凭证日期
        voucherMaster.put("unitcode", cwsf.getCreateOrgan() != null && cwsf.getCreateOrgan().getJiuqiUnitCode() != null ? cwsf.getCreateOrgan().getJiuqiUnitCode() : "");//组织机构代码
        voucherMaster.put("acctbookcode", "01");//账薄标识
        voucherMaster.put("createuser", yg.getXm());//制单人
        voucherMaster.put("attachment", attachment);//附件张数
        voucherMaster.put("memo", "");//备注
        voucherMaster.put("companyId", this.findskdrDetail(skdrId).getSbgs().getId());//公司ID
        return voucherMaster;
    }

    //财务收付分录
    public String findSkdrfl(Long skdrId) {
        Sfk cwsf = this.skdrDao.get(skdrId);
        List<Map> list = this.skdrDao.getVoucher(cwsf);
        List<Map> txcdJson = new ArrayList<Map>();
        Double credit = 0d;
        Double debit = 0d;
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> voucherItem = (Map<String, Object>) list.get(i);
            voucherItem.put("zy", voucherItem.get("digest"));
            voucherItem.put("km", voucherItem.get("subjectname"));
            voucherItem.put("jf", voucherItem.get("debit"));
            voucherItem.put("df", voucherItem.get("credit"));
            debit += Double.valueOf((voucherItem.get("natural_debit_currency") == null ? 0d : voucherItem.get("natural_debit_currency")).toString());
            credit += Double.valueOf((voucherItem.get("natural_credit_currency") == null ? 0d : voucherItem.get("natural_credit_currency")).toString());
            txcdJson.add(voucherItem);
        }
        String total = "合计：" + MoneyUtils.change(debit);
        return "{\"rows\":" + new Gson().toJson(txcdJson) + ",\"footer\":[{\"km\":\"" + total + "\",\"natural_debit_currency\":" + debit + ",\"natural_credit_currency\":" + credit + "}]}";
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }

    @Autowired
    public void setSkdrDao(SkdrDao skdrDao) {
        this.skdrDao = skdrDao;
    }

}
